#ifndef __PACKET_H_
#define __PACKET_H_

#define EMRTDATAHEADER    (0x454D3031)
#define EMPACKET_BUFFSIZE (16+24+(40*18)+(24*32))

#define APDU_MAXLEN       4096

#define COMMAND_READ      0x00
#define COMMAND_WRITE     0x01
#define COMMAND_CONTROL   0x02
#include <stdint.h>

#define PROTOCOL_CAN      0x01
#define PROTOCOL_LIN      0x02
#define PROTOCOL_DO       0x03
#define PROTOCOL_LED      0x04
#define PROTOCOL_RTC      0x05
#define PROTOCOL_DOMENCY  0x06


struct com_packet_addrdesc
{
  uint32_t offset;
  uint32_t num;
};

struct com_packet 
{
  uint64_t timestamp;
  uint32_t id;
  uint16_t dlc;
  uint8_t channel;
  uint8_t flag;
  uint8_t data[64];
};

// struct canfd_frame {
//         canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
//         __u8    len;     /* frame payload length in byte (0 .. 64) */
//         __u8    flags;   /* additional flags for CAN FD */
//         __u8    __res0;  /* reserved / padding */
//         __u8    __res1;  /* reserved / padding */
//         __u8    data[64] __attribute__((aligned(8)));
// };

// struct can_frame {
//         canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
//         union {
//                 /* CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
//                  * was previously named can_dlc so we need to carry that
//                  * name for legacy support
//                  */
//                 __u8 len;
//                 __u8 can_dlc; /* deprecated */
//         };
//         __u8    __pad;   /* padding */
//         __u8    __res0;  /* reserved / padding */
//         __u8    len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
//         __u8    data[8] __attribute__((aligned(8)));
// };

struct vio_status
{
  uint64_t timestamp;
  uint32_t id;
  uint16_t dlc;
  uint8_t channel;
  uint8_t flag;
  int16_t data[4];
};

struct com_status
{
  uint64_t timestamp;
  uint32_t id;
  uint16_t dlc;
  uint8_t channel;
  uint8_t flag; 
  uint8_t data[24];
};

struct com_event
{
  uint64_t timestamp;
  uint32_t id;
  uint16_t dlc;
  uint8_t channel;
  uint8_t flag;
  uint8_t data[8];
};

struct em_packet
{
  uint32_t header;
  uint8_t  pacnum;
  uint8_t  vionum;
  uint8_t  statusnum;
  uint8_t  eventnum;
  uint8_t  buffer[EMPACKET_BUFFSIZE];
};

struct can_param
{
  uint16_t norm_div;
  uint16_t norm_prop_seg;
  uint16_t norm_phase_seg1;
  uint16_t norm_phase_seg2;
  uint16_t fast_div;
  uint16_t fast_prop_seg;
  uint16_t fast_phase_seg1;
  uint16_t fast_phase_seg2;
  uint8_t  norm_sjw;
  uint8_t  fast_sjw;
  uint8_t  reserved0;
  uint8_t  reserved1;
};

#define EMREQUEST_DATASIZE  (2560)
struct em_request
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint8_t data[EMREQUEST_DATASIZE];
};

struct em_rsponse
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint8_t data[12];
};

struct gps_data
{
  uint64_t timestamp;
  uint32_t gpstime;//Byte2~Byte1:时分秒
  uint8_t state; //1有效，0无效
  uint8_t satelltes; //有效卫星数
  uint8_t nsdir; //南/北纬(1/0)
  uint8_t ewdir; //东/西经(1/0)
  float latitude; //纬度
  float longtitude; //经度
  float altitude; //海拔高度;
  float speed; //gps速度(km/s)
  float direction; //方位角，导航角
};

struct rmu_state
{
  uint64_t timestamp;
  uint8_t wakesrc;//唤醒源
  uint8_t canmode; //16路can的工作模式(总开关)
  uint8_t distate; //4路数字量输入状态
  uint8_t dostate; //4路数字量输出开关
  int16_t obdvolt; //OBD电压
  int16_t batteryvolt; //电池电压
  uint64_t ledstate; //led1~led18 状态
  int16_t hs_temp; //高速板温度
  int16_t ls_temp; //低速板温度
  int16_t batterytemp; //电池温度
  int16_t doiplinevolt; //DoIP active line 
  int16_t hsd_adc[2]; //2路hsd电压采样值
  int16_t lsd_adc[2]; //2路lsd电压采样值
  int16_t ai; //一路ai输入电压采样值
  uint16_t battchargecount; //电池充电次数
  uint8_t sysstate; //系统状态
  uint8_t battoutstatus; //电池拔出状态
  uint8_t battchargestatus; //电池充电状态
  uint8_t ignstate; //ign线状态，1有效，0无效
  uint8_t obd_outstate; //obd拔出标志
  uint8_t otastate; //k3OTA状态
  uint8_t k3_crcerr; //k3 crc
  uint8_t reserved;
  char a53appver[24];
  char m7fwver[24];
  char k3fwver[24];
};

struct cantp_connect_req
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint32_t phyreqid;
  uint32_t phyrspid;
  uint32_t funcreqid;
  uint32_t canmode;
};

struct cantp_connect_rsp
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint32_t handle;
  int16_t error;
};

struct cantp_req
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint32_t handle;
  uint16_t txparam; //bit15 = 1 used functional canid bit0~bit12 apdu length
  uint8_t data[4096];
};

struct cantp_rsp
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint32_t handle;
  int16_t error;
  uint16_t rxparam; //apdu len
  uint8_t data[256]; //256
};

struct cantp_close_req
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint32_t handle;
};

struct cantp_close_rsp
{
  uint8_t command;
  uint8_t protocol;
  uint8_t channel;
  uint8_t param;
  uint32_t handle;
  int16_t error;
};

#endif
